(function() {

	var app = angular.module('babyApp');

	app.config(function($httpProvider) {
		// Enable cross domain calls
		$httpProvider.defaults.useXDomain = true;

		// Remove the header used to identify ajax call that would prevent CORS from working
		delete $httpProvider.defaults.headers.common['X-Requested-With'];
	});

	app.controller('FetchController', [ '$scope', '$http', 'dateFilter', function($scope, $http, dateFilter) {

		$scope.logs = [];

		// /////////////////////////////////////////////
		$scope.fetchLog = function() {

			delete $http.defaults.headers.common['X-Requested-With'];

			// var localUrl = 'http://localhost:7001/baby-rest-services/log/get/';
			var localUrl = 'http://localhost:8080/log/get';
			// var localUrl = 'http://default-environment-2xgz38vab8.elasticbeanstalk.com/log/get';

			$http.get(localUrl, {
				headers : {
					'Accept' : 'application/json',
					'Access-Control-Allow-Origin' : '*'
				}
			}).success(function(data, status, headers, config) {

				$scope.logs = (data);
			}).error(function(data, status, headers, config) {

				$scope.logs = "error: [" + angular.toJson(status) + "] " + angular.toJson(data);
			});

		};

		$scope.deleteLog = function(id) {

			delete $http.defaults.headers.common['X-Requested-With'];

			// var localDeleteUrl = 'http://localhost:7001/baby-rest-services/log/delete/' + id;
			var localDeleteUrl = 'http://localhost:8080/log/delete/' + id;
			// var localDeleteUrl = 'http://default-environment-2xgz38vab8.elasticbeanstalk.com/log/delete/' + id;

			$http.post(localDeleteUrl, angular.toJson($scope.newLog)) //
			.success(function(data, status, headers, config) {

				$scope.fetchLog();

			}).error(function(data, status, headers, config) {

				alert('Error [' + status + ']');

			});
		};

		$scope.fetchLog();

	} ]);

})();